var zTree;
$(document)
		.ready(
				function() {

					$.ajax({
						url : sy.bp() + "/syorg/selectAll.action",
						type : "post",
						dataType : "json",
						success : function(a) {
							t = $.fn.zTree.init($("#syorgztree"), set, a);
							console.info(a);
							var treeObj = $.fn.zTree.getZTreeObj("syorgztree");
							treeObj.expandAll(true);
						}
					});
					
					
						
					
					
					var set = {
						view : {
							dblClickExpand : true,// 双击节点时，是否自动展开父节点的标识
							
							showLine : true,// 是否显示节点之间的连线
							fontCss : {
								'color' : 'blue',
								'font-weight' : 'bold'
							},// 字体样式函数
							selectedMulti : true,
							// 设置是否允许同时选中多个节点

							addHoverDom : addHoverDom,
							removeHoverDom : removeHoverDom
						},
						check : {
							chkStyle : "checkbox",
							enable : false
						},
						data : {
							simpleData : {// 简单数据模式
								enable : true,
								idKey : "id",
								pIdKey : "pid",
								rootPId : ""
							}
						},
						callback : {
							beforeRemove : zTreeOnRemove,
							 beforeEditName:zTreeBeforeEidtName,
						},
						edit : {
							enable : true,
							showRemoveBtn : true,
							removeTitle : "删除节点",
							showRenameBtn : true,
							renameTitle : "编辑节点名称",
						}
					}

					function addHoverDom(treeId, treeNode) {
						// console.info(treeNode);
						
						var addAuthority = "添加节点";
						var aObj = $("#" + treeNode.tId + "_a");
				
						if ($("#diyBtn_" + treeNode.id).length > 0)
							return;
						var editStr = "<span></span>"
								+ "<button type='button' class='addHoverDom' id='diyBtn_"
								+ treeNode.id + "' title='" + addAuthority
								+ "' onfocus='this.blur();'>+</button>";
						aObj.append(editStr);
						var btn = $("#diyBtn_" + treeNode.id);
						if (btn)
							btn.bind("click", function() {
								alert(treeNode.id);
								alert(treeNode.pid);
								// alert("diy Button for " + treeNode.name);
								$("#id").val(treeNode.id);
								$("#seq").val(treeNode.seq);
								$("#pid").val(treeNode.pid);
								$('#myModal-add-info').modal('show');
							});
						return false;
					}
					;
					function removeHoverDom(treeId, treeNode) {
						$("#diyBtn_" + treeNode.id).unbind().remove();
						$("#diyBtn_space_" + treeNode.id).unbind().remove();
					}
					;

					function zTreeOnRemove(treeId, treeNode) {
						// console.info(treeId);
						// console.info(treeNode.id);
						$('#deleteOneModal').modal('show');
						var id = treeNode.id;
						$("#syorgId").val(id);
						return false;
					}

					function zTreeBeforeEidtName( treeId, treeNode) {
						// console.info(treeId.id);
						// console.info(treeNode);
						var url = sy.bp() + "/syorg/selectByPrimaryKey.action?";
						$.ajax({
							url : url,
							type : "post",
							data : {
								"id" : treeNode.id,
							},
							dataType : "json",
							success : function(data) {
								$("#treeNodeId").val(data.id), $(
										"#treeNodeName").val(data.name), $(
										"#treeNodePid").val(data.pid), $(
										"#treeNodeSrc").val(data.src), $(
										'#myModal-update-info').modal('show');
								return false;
							}
						});
					}

				});

function deletesyorgtj() {
	var id = $("#syorgId").val();
	alert(id);
	var url = sy.bp() + "/syorg/syorgdelete.action?id=" + id;
	$.ajax({
		url : url,
		type : "get",
		dataType : "json",
		success : function(data) {
			console.info(data);
			if (data == 1) {
				alert("删除成功！");
				window.location.reload();
				return true;
			} else {
				alert("删除失败！");
				return false;
			}
		}

	});

}

function updatesyorgtj() {
	var url = sy.bp() + "/syorg/updatesyorg.action";
	alert($("#treeNodeId").val());
	alert($("#treeNodeName").val());
	$.ajax({
		url : url,
		type : "post",
		data : {
			"id" : $("#treeNodeId").val(),
			"pid" : $("#treeNodePid").val(),
			"name" : $("#treeNodeName").val(),
			"src" : $("#treeNodeSrc").val(),
		},
		dataType : "json",
		success : function(data) {
			console.info(data);
			if (data == 1) {
				alert("修改成功！");
				$('#myModal-update-info').modal('hide');
				window.location.reload();
				return true;
			} else {
				alert("修改失败！");
				window.location.reload();
				return false;
			}
			

		}
	});

}

function insertsyorgtj() {
	// alert($("#cid").val());
	var url = sy.bp() + "/syorg/insertsyorg.action";
	$.ajax({
		url : url,
		type : "post",
		data : {
			"pid" : $("#id").val(),
			"name" : $("#nameid").val(),
			"orgdetail" : $("#orgdetailid").val(),
		},
		dataType : "json",
		success : function(data) {
			console.info(data);
			if (data == 1) {
				alert("添加成功！");
				$('#myModal-add-info').modal('hide');
				window.location.reload();
				return true;
			} else {
				alert("添加失败！");
				window.location.reload();
				return false;

			}

		}
	});

}

function exit() {
	window.location.reload();
}

/*
 * success:function(data){ $("#treeNodeId").val(data.record.id),
 * $("#treeNodeName").val(data.record.name),
 * //$('#myModal-update-info').modal.('show'), }
 */